{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# docker 基础命令学习\n",
    "\n",
    "> * Docker 使用客户端-服务器 (C/S) 架构模式，使用 remote API 来管理和创建 Docker 容器\n",
    "\n",
    "* 镜像 - images - 是用于创建 Docker 容器的模板\n",
    "* 容器 - container - 是独立运行的一个或一组应用，是镜像运行时的实体\n",
    "  * 镜像和容器关系类似类和实例关系；镜像是静态的，容器是可以被创建、启动、停止、删除、暂停等操作\n",
    "* 仓库 - repository - 用来保存镜像，可以理解为代码控制中的代码仓库\n",
    "  * Docker Hub、阿里云，腾讯云容器镜像服等等\n",
    "  \n",
    "**其他术语解释**\n",
    "> * Docker 客户端 - 通过命令行或者其他工具使用 Docker SDK (https://docs.docker.com/develop/sdk/) 与 Docker 的守护进程通信\n",
    "> * Docker 主机 - 一个物理或者虚拟的机器用于执行 Docker 守护进程和容器\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 镜像\n",
    "\n",
    "### 获取镜像  -  docker pull\n",
    "\n",
    "* 从仓库注册服务器（registry）拉取一个镜像或一个仓库\n",
    "* docker pull [options] name[:tag|@digest]\n",
    "* **options**\n",
    "  * -a, --all-tags[=false] - 下载仓库中所有标签符合的镜像\n",
    "  * --disable-content-trust[=true] - 跳过镜像内容验证\n",
    "  * --platform=\"\" - 设置平台，如果服务器是多平台的\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE\r\n",
      "<none>              <none>              0e2a3d5a2bf6        11 hours ago        64.2MB\r\n",
      "ubuntu              18.04               cf0f3ca922e0        12 days ago         64.2MB\r\n"
     ]
    }
   ],
   "source": [
    "! docker images"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "18.04: Pulling from library/ubuntu\n",
      "\n",
      "\u001b[1B16666fd6: Pulling fs layer \n",
      "\u001b[1B6d2a1e53: Pulling fs layer \n",
      "\u001b[1B8ebae908: Pulling fs layer \n",
      "\u001b[1B94023a2e: Downloading  26.22MB/26.69MB\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2KDownloading  24.05MB/26.69MB\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[1A\u001b[2K18.04: Pulling from library/ubuntu\n",
      "\n",
      "\u001b[1B16666fd6: Pulling fs layer \n",
      "\u001b[1B6d2a1e53: Pulling fs layer \n",
      "\u001b[1B8ebae908: Pulling fs layer \n",
      "\u001b[1B94023a2e: Pull complete  162B/162B7kBBA\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[3A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[4A\u001b[2K\u001b[3A\u001b[2K\u001b[3A\u001b[2K\u001b[2A\u001b[2K\u001b[2A\u001b[2K\u001b[1A\u001b[2K\u001b[1A\u001b[2KDigest: sha256:a7b8b7b33e44b123d7f997bd4d3d0a59fafc63e203d17efedf09ff3f6f516152\n",
      "Status: Downloaded newer image for ubuntu:18.04\n",
      "docker.io/library/ubuntu:18.04\n"
     ]
    }
   ],
   "source": [
    "! docker pull ubuntu:18.04"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE\r\n",
      "ubuntu              18.04               cf0f3ca922e0        11 days ago         64.2MB\r\n"
     ]
    }
   ],
   "source": [
    "! docker images"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 镜像添加标签 - docker tag\n",
    "\n",
    "* **docker tag source_image[:tag] target_image[:tag]**\n",
    "\n",
    "``` shell\n",
    "$ docker images\n",
    "REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE\n",
    "docker.io/busybox   latest              020584afccce        2 weeks ago         1.22 MB\n",
    "$ docker tag docker.io/busybox:latest busybox:latest\n",
    "$ docker images\n",
    "REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE\n",
    "busybox             latest              020584afccce        2 weeks ago         1.22 MB\n",
    "docker.io/busybox   latest              020584afccce        2 weeks ago         1.22 MB\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 创建镜像 - docker commit / import / build\n",
    "\n",
    "1. **基于修改已有的镜像的容器创建 - docker commit**\n",
    "\n",
    "\n",
    "* **docker commit [options] container [repository[:tag]]**\n",
    "* **[options]**\n",
    "  * -a, --author=\"\"\n",
    "        Author (e.g., \"John Hannibal Smith ⟨hannibal@a-team.com⟩\")\n",
    "  * -c, --change=\n",
    "        Apply Dockerfile instruction to the created image\n",
    "  * -h, --help[=false]\n",
    "        help for commit\n",
    "  * -m, --message=\"\"\n",
    "        Commit message\n",
    "  * -p, --pause[=true]\n",
    "        Pause container during commit\n",
    "        \n",
    "``` shell\n",
    "$ docker run -it ubuntu:18.04 /bin/bash\n",
    "root@d889c6bec6ad:/# touch test.txt\n",
    "root@d889c6bec6ad:/# exit\n",
    "exit\n",
    "$ commit -m \"Added a new file\" -a \"Docker\" d889c6bec6ad\n",
    "commit：未找到命令\n",
    "$ docker commit -m \"Added a new file\" -a \"Docker\" d889c6bec6ad\n",
    "sha256:0e2a3d5a2bf658873f9cca3ce4f602138b10e7180908f359cd95c763e0741761\n",
    "$ docker images\n",
    "REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE\n",
    "<none>              <none>              0e2a3d5a2bf6        8 seconds ago       64.2MB\n",
    "ubuntu              18.04               cf0f3ca922e0        11 days ago         64.2MB\n",
    "```\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2. 基于本地模版导入 - **docker import**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3. 基于 Dockerfile 创建 - **docker build**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###  列出 Docker 镜像  - docker images"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE\r\n",
      "ubuntu              18.04               cf0f3ca922e0        3 weeks ago         64.2MB\r\n",
      "centos              7                   67fa590cfc1c        2 months ago        202MB\r\n",
      "v2ray/official      latest              1f7d9ffa0ab8        3 months ago        66.6MB\r\n",
      "jumperserver        1.0.0               055f42f305f5        20 months ago       1.41GB\r\n"
     ]
    }
   ],
   "source": [
    "# 使用 Images 命令列出镜像信息\n",
    "! docker images"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "cf0f3ca922e0\r\n",
      "67fa590cfc1c\r\n",
      "1f7d9ffa0ab8\r\n",
      "055f42f305f5\r\n"
     ]
    }
   ],
   "source": [
    "# 列出所有镜像的 ID\n",
    "! docker images -q"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "REPOSITORY          TAG                 DIGEST                                                                    IMAGE ID            CREATED             SIZE\r\n",
      "ubuntu              18.04               sha256:a7b8b7b33e44b123d7f997bd4d3d0a59fafc63e203d17efedf09ff3f6f516152   cf0f3ca922e0        3 weeks ago         64.2MB\r\n",
      "centos              7                   sha256:307835c385f656ec2e2fec602cf093224173c51119bbebd602c53c3653a3d6eb   67fa590cfc1c        2 months ago        202MB\r\n",
      "v2ray/official      latest              sha256:1b3e47d56bb2e012b4a3dfad53e9385cd04cf85025eb504d0039eff046589507   1f7d9ffa0ab8        3 months ago        66.6MB\r\n",
      "jumperserver        1.0.0               <none>                                                                    055f42f305f5        20 months ago       1.41GB\r\n"
     ]
    }
   ],
   "source": [
    "# 列出所有镜像的数字摘要\n",
    "! docker images --digests=true"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 删除一个或多个镜像  -  docker rmi 或 docker image rm\n",
    "\n",
    "* **docker rmi [options] image1 [image2...]**\n",
    "* **options**\n",
    "  * -f, --force[=false]\n",
    "        强制删除镜像，即使有容器依赖它\n",
    "  * -no-prune[=false]\n",
    "        不要删除未带标签（tag）的父镜像\n",
    "  \n",
    "``` shell\n",
    "# 需要先删除以该镜像为基础的容器，才能顺利删除镜像\n",
    "$ docker rmi ubuntu:18.04\n",
    "Error response from daemon: conflict: unable to remove repository reference \"ubuntu:18.04\" (must force) - container 6416942ec51e is using its referenced image cf0f3ca922e0\n",
    "$ docker rm ubuntuTest\n",
    "ubuntuTest\n",
    "$ docker rmi ubuntu:18.04\n",
    "Untagged: ubuntu:18.04\n",
    "Deleted: sha256:cf0f3ca922e08045795f67138b394c7287fbc0f4842ee39244a1a1aaca8c5e1c\n",
    "Deleted: sha256:c808877c0adcf4ff8dcd2917c5c517dcfc76e9e8a035728fd8f0eae195d11908\n",
    "Deleted: sha256:cdf75cc6b4d28e72a9931be2a88c6c421ad03cbf984b099916a74f107e6708ff\n",
    "Deleted: sha256:b9997ded97a1c277d55be0d803cf76ee6e7b2e8235d610de0020a7c84c837b93\n",
    "Deleted: sha256:a090697502b8d19fbc83afb24d8fb59b01e48bf87763a00ca55cfff42423ad36\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###  搜索镜像（可添加筛选条件） - docker search "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "NAME                 DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED\r\n",
      "ubuntu               Ubuntu is a Debian-based Linux operating sys…   10089               [OK]                \r\n",
      "ubuntu-upstart       Upstart is an event-based replacement for th…   100                 [OK]                \r\n",
      "ubuntu-debootstrap   debootstrap --variant=minbase --components=m…   40                  [OK]                \r\n"
     ]
    }
   ],
   "source": [
    "# search 搜索  Docker Hub 官方仓库里的镜像\n",
    "! docker search ubuntu --filter=is-official=true"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "NAME                             DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED\r\n",
      "ubuntu                           Ubuntu is a Debian-based Linux operating sys…   10159               [OK]                \r\n",
      "dorowu/ubuntu-desktop-lxde-vnc   Docker image to provide HTML5 VNC interface …   361                                     [OK]\r\n",
      "rastasheep/ubuntu-sshd           Dockerized SSH service, built on top of offi…   234                                     [OK]\r\n",
      "consol/ubuntu-xfce-vnc           Ubuntu container with \"headless\" VNC session…   194                                     [OK]\r\n",
      "ubuntu-upstart                   Upstart is an event-based replacement for th…   100                 [OK]                \r\n"
     ]
    }
   ],
   "source": [
    "# 匹配 stars 多于 100\n",
    "! docker search ubuntu --filter=stars=100"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 打包一个或多个镜像成 tar 压缩包  -  docker save\n",
    "\n",
    "* **docker save [options] image1 [image2...]**\n",
    "* **[options]**\n",
    "  * -o, --output=\"\"\n",
    "        导出镜像到指定文件\n",
    "  \n",
    "  \n",
    "``` shell\n",
    "$ docker save -o ubuntu_18.04.tar ubuntu:18.04\n",
    "# docker save ubuntu:18.04 > ubuntu-18.04.tar\n",
    "$ ls\n",
    "ubuntu_18.04.tar\n",
    "```\n",
    "\n",
    "### 从 tar 包或 STDIN 载入一个镜像 - docker load\n",
    "\n",
    "* **docker load [options]**\n",
    "* **[options]**\n",
    "  * -i, --input=\"\" - 从 tar 包文件载入，而不是从 STDIN\n",
    "  \n",
    "  \n",
    "``` shell\n",
    "docker load -i ubuntu_18.04.tar\n",
    "# docker load < ubuntu_18.04.tar\n",
    "Loaded image: ubuntu:18.04\n",
    "```\n",
    "\n",
    "### 上传一个镜像或一个仓库到镜像注册服务器  -  docker login / push\n",
    "\n",
    "* **docker push [options] name[:tag] | [registry_host][:registry_post]/]name[:tag]**\n",
    "* 上传镜像必须是 `dockerHubUsername/RepoName` 格式\n",
    "* 可以先 `docker login` 登录 dockerHub\n",
    "\n",
    "``` shell\n",
    "$ docker images\n",
    "REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE\n",
    "xiechengqi/test     latest              4a5736a985c4        8 minutes ago       1.22 MB\n",
    "$ docker login\n",
    "Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.\n",
    "Username: xiechengqi\n",
    "Password: \n",
    "Login Succeeded\n",
    "$ docker push xiechengqi/test\n",
    "The push refers to a repository [docker.io/xiechengqi/test]\n",
    "6fe5f95797a0: Pushed \n",
    "1da8e4c8d307: Pushed \n",
    "latest: digest: sha256:c6ef060b9a96504950198d4414fa077458fff030920b4f882cb08f838c8c758f size: 734\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 二、容器\n",
    "\n",
    "### 创建容器  -  docker create / run\n",
    "\n",
    "* **docker create [options] image [command] [arg1,arg2...]**\n",
    "* 命令别名 - `docker container create`\n",
    "* **[options]**\n",
    "  * -i, --interactive[=false]\n",
    "        保持 STDIN 开启，即使未连接上容器\n",
    "  * -t, --tty[=false]\n",
    "        分配一个模拟的 TTY 终端\n",
    "        \n",
    "        \n",
    "``` shell\n",
    "# 新建一个容器\n",
    "docker create -it ubuntuTest\n",
    "5c716348d0defff481b571bc3a2b5a1f8844414799ab2d8bb0472c2e9512ee73\n",
    "\n",
    "# 直接新建容器并启动\n",
    "# 1. 本地没有 image ，从公有仓库下载\n",
    "# 2. 创建一个容器，并启动该容器\n",
    "# 3. 分配一个文件系统给容器，并在只读的镜像层外面挂载一层可读写层\n",
    "# 4. 从宿主主机配置的网桥接口中桥接一个虚拟接口到容器中去\n",
    "# 5. 从网桥的地址池配置一个 IP 地址给容器\n",
    "# 6. 执行用户指定的应用程序\n",
    "# 7. 执行完毕后自动终止\n",
    "$ docker run --name myubuntu -i -t ubuntu:18.04 /bin/bash\n",
    "Unable to find image 'ubuntu:18.04' locally\n",
    "Trying to pull repository docker.io/library/ubuntu ... \n",
    "18.04: Pulling from docker.io/library/ubuntu\n",
    "7ddbc47eeb70: Pull complete \n",
    "c1bbdc448b72: Pull complete \n",
    "8c3b70e39044: Pull complete \n",
    "45d437916d57: Pull complete \n",
    "Digest: sha256:6e9f67fa63b0323e9a1e587fd71c561ba48a034504fb804fd26fd8800039835d\n",
    "Status: Downloaded newer image for docker.io/ubuntu:18.04\n",
    "root@2a92b35a1171:/# cat /etc/issue\n",
    "Ubuntu 18.04.3 LTS \\n \\l\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 列出容器  -  docker ps\n",
    "\n",
    "* **docker ps [options]**\n",
    "* **[options]**\n",
    "  * -a, --all=true|false\n",
    "          Show all containers. Only running containers are shown by default. The default is false.\n",
    "  * -q, --quiet=true|false\n",
    "          Only display numeric IDs. The default is false.\n",
    "  * -s, --size=true|false\n",
    "          Display total file sizes. The default is false."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CONTAINER ID        IMAGE                COMMAND               CREATED             STATUS                    PORTS               NAMES\r\n",
      "0da402a2df87        jumperserver:1.0.0   \"/opt/start_jms.sh\"   5 days ago          Exited (137) 5 days ago                       jms_server\r\n",
      "ba1022135516        jumperserver:1.0.0   \"/bin/bash\"           5 days ago          Exited (137) 5 days ago                       intelligent_jepsen\r\n",
      "a77a6e63fd35        ubuntu:18.04         \"/bin/bash\"           2 weeks ago         Exited (100) 7 days ago                       ecstatic_nightingale\r\n",
      "a81da36d5420        centos:7             \"--name centos_js\"    2 weeks ago         Created                                       happy_mcnulty\r\n"
     ]
    }
   ],
   "source": [
    "# 查看所有容器 ID\n",
    "! docker ps -a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0da402a2df87\r\n",
      "ba1022135516\r\n",
      "a77a6e63fd35\r\n",
      "a81da36d5420\r\n"
     ]
    }
   ],
   "source": [
    "# 查看所有容器 ID\n",
    "! docker ps -qa"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 重命名一个容器 - docker rename\n",
    "\n",
    "* **docker rename container new_name**\n",
    "\n",
    "``` shell\n",
    "$ docker ps -a\n",
    "CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES\n",
    "6416942ec51e        ubuntu:18.04        \"/bin/bash\"         13 minutes ago      Exited (0) 3 minutes ago                       myubuntu\n",
    "$ docker rename 6416942ec51e ubuntuTest\n",
    "$ docker ps -a\n",
    "CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES\n",
    "6416942ec51e        ubuntu:18.04        \"/bin/bash\"         14 minutes ago      Exited (0) 4 minutes ago                       ubuntuTest\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 暂停、继续一个或多个容器中的所有进程 - docker pause / unpause\n",
    "\n",
    "* **docker pause container1 [container2...]**\n",
    "* **docker unpause container1 [container2...]**\n",
    "\n",
    "``` shell\n",
    "$ docker ps\n",
    "CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS              PORTS               NAMES\n",
    "6416942ec51e        ubuntu:18.04        \"/bin/bash\"         About a minute ago   Up About a minute                       ubuntuTest\n",
    "$ docker pause ubuntuTest\n",
    "ubuntuTest\n",
    "$ docker ps\n",
    "CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS                       PORTS               NAMES\n",
    "6416942ec51e        ubuntu:18.04        \"/bin/bash\"         About a minute ago   Up About a minute (Paused)                       ubuntuTest\n",
    "$ docker unpause ubuntuTest\n",
    "ubuntuTest\n",
    "$ docker ps\n",
    "CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES\n",
    "6416942ec51e        ubuntu:18.04        \"/bin/bash\"         8 minutes ago       Up 8 minutes                            ubuntuTest\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 终止一个或多个正在运行的容器 - docker stop\n",
    "\n",
    "* **docker stop [options] container1 [container2...]**\n",
    "* 只能终止**正在运行**的容器，不可以结束暂停 ( pause ) 的容器\n",
    "* **options**\n",
    "  * -t, --time=10\n",
    "        等待 10 秒再终止\n",
    "  \n",
    "``` shell\n",
    "$ docker ps\n",
    "CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES\n",
    "6416942ec51e        ubuntu:18.04        \"/bin/bash\"         9 minutes ago       Up 9 minutes                            ubuntuTest\n",
    "$ docker stop ubuntuTest\n",
    "ubuntuTest\n",
    "$ docker ps\n",
    "CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 重启一个或多个容器 - docker restart\n",
    "\n",
    "* **docker restart [options] container1 [container2...]**\n",
    "* 至少一个参数\n",
    "* **[options]**\n",
    "  * -t, --time=10\n",
    "        等待 10 秒再重启"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 返回 Docker 对象底层信息  -  docker inspect\n",
    "\n",
    "* **docker inspect [options] name1|id1 [name2|id2 ...]**\n",
    "* 至少一个参数\n",
    "* **对象包括：container \\ image \\ volume \\ network \\ node \\ service \\ task**\n",
    "* 会以 json 格式返回包括对象 ID、创建时间、路径、状态、镜像、配置等在内的各项信息\n",
    "* **[options]**\n",
    "  * -s, --size[=false]\n",
    "        如果是容器，则显示全部文件的大小"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[]\r\n",
      "Error: No such object: e3\r\n"
     ]
    }
   ],
   "source": [
    "# 使用 inspect 命令查看详细信息\n",
    "! docker inspect e3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 显示一个运行中的容器中正在运行的进程  -  docker top\n",
    "\n",
    "* **docker top container [ps options]**\n",
    "* 至少一个参数\n",
    "\n",
    "``` shell\n",
    "docker top busyboxTest\n",
    "UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD\n",
    "root                27040               27027               0                   09:53               pts/2               00:00:00            sh\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 显示一个运行中的容器资源使用情况 - docker stats\n",
    "\n",
    "* **docker stats [options] [container1, container2...]**\n",
    "* **正在运行中容器**\n",
    "* **资源包括：CPU、内存、存储、网络等**\n",
    "* **[options]**\n",
    "  * -a, --all[=false]\n",
    "        输出所有容器 stats\n",
    "  * --format=\"\"\n",
    "        使用 Go 模版美化输出图片\n",
    "  * --no-stream[=false]\n",
    "        关闭流状态展示，且只截取执行命令后第一个状态信息\n",
    "  * --no-trunc\n",
    "        不截断输出信息\n",
    "\n",
    "``` shell\n",
    "$ docker stats --no-stream\n",
    "CONTAINER           CPU %               MEM USAGE / LIMIT    MEM %               NET I/O             BLOCK I/O           PIDS\n",
    "1c85e7076e6f        0.00%               56 KiB / 1.796 GiB   0.00%               0 B / 648 B         0 B / 0 B           1\n",
    "```"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
